package View;

import java.util.ArrayList;
import java.util.Scanner;

import Controller.*;
import Helper.Helper;
import Model.*;

public class Index {
    public static void index(){
        Helper.clear();
        System.out.println("*-----[ 欢迎进入电子商城 ]-----*\n");

        System.out.println("\t 1. 登录");
        System.out.println("\t 2. 注册\n");

        System.out.println("\t 3. 浏览商品");
        System.out.println("\t 4. 搜索商品\n");

        System.out.println("\t 5. 退出");

        System.out.println("*---------------------------*");
    }

    public static int login(){
        Scanner scanner = new Scanner(System.in);

        System.out.println("[ 登录 ]\n");

        System.out.println("请选择登录类型：\n");
        System.out.println("0. 买家");
        System.out.println("1. 卖家");
        System.out.println("2. 管理员\n");

        System.out.print("登录类型：");

        String type = scanner.next();
        while( !type.matches("[0-2]") ){
            System.err.println("只能输入0~2之间的数字！");
            System.out.print("请重新选择类型： ");

            type = scanner.next();
        }


        System.out.print("用户名: ");

        String username = scanner.next();
        while( !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只允许由字母数字下划线组成！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        int status = -1;
        switch (type) {
            case "0":
                if (UserController.login(username, password)) {
                    status = 0;
                }
            break;
            case "1":
                if (VendorController.login(username, password)) {
                    status = 1;
                }
            break;
            case "2":
                if (AdminController.login(username, password)) {
                    status = 2;
                }
            break;
        }

        return status;
    }

    public static boolean signup(){
        Scanner scanner = new Scanner(System.in);

        System.out.println("[ 注册 ]");

        System.out.println("请选择登录类型：\n");
        System.out.println("0. 买家");
        System.out.println("1. 卖家\n");

        System.out.print("登录类型：");

        String type = scanner.next();
        while( !type.matches("[0-1]") ){
            System.err.println("只能输入0~1之间的数字！");
            System.out.print("请重新选择类型： ");

            type = scanner.next();
        }

        System.out.print("用户名: ");

        String username = scanner.next();
        while( username.length()>50 || !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只能由字母数字下划线组成，且小于50字节！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        while( !Helper.getPassword("确认密码: ").equals(password) ){
            System.err.println("两次密码不一致，需重新设置密码！");

            password = Helper.getPassword("密码: ");
        }

        if( !Helper.brancher("确定？", true) ){
            return false;
        }

        boolean status;

        switch (type) {
            case "0":
                status = UserController.signup(username, password);
                break;
            case "1":
                status = VendorController.signup(username, password);
                break;
            default:
                status = false;
        }

        if( status ){
            System.out.println("注册成功！");
        }else{
            System.err.println("注册失败！");
        }

        Helper.waitter();

        return status;
    }

    public static void listProducts() {
        Helper.listProducts();
    }

    public static void searchProduct() {
        Helper.searchProduct();
    }

    public static boolean adminLogin(){
        System.out.println("[ 管理员登录 ]\n");
        System.out.print("用户名: ");

        Scanner scanner = new Scanner(System.in);
        String username = scanner.next();
        while( !username.matches("[0-9a-zA-Z_]+") ){
            System.err.println("用户名只允许由字母数字下划线组成！");
            System.out.print("请重新输入用户名： ");

            username = scanner.next();
        }

        String password = Helper.getPassword("密码: ");

        return VendorController.login(username, password);
    }
}
